package com.yanqu.road.dao.impl.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.forbiddenword.ForbiddenWordOverSeasData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class ForbiddenWordOverSeasDaoImpl extends TempDataObjectDao<ForbiddenWordOverSeasData> {

    public static String getForbiddenWordOverSeasDataSql = "select * from `t_s_forbidden_word_overseas`";
    public List<ForbiddenWordOverSeasData> getForbiddenWordOverSeasData() {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ForbiddenWordOverSeasData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getForbiddenWordOverSeasDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                dataList.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateForbiddenWordOverSeasDataSql = "update `t_s_forbidden_word_overseas` set wordSet=? where id=?";
    @Override
    protected boolean updateData(ForbiddenWordOverSeasData forbiddenWordOverSeasData) {
        forbiddenWordOverSeasData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(forbiddenWordOverSeasData.getWordSet())));
        para.put(i++, new DbParameter(Types.INTEGER, forbiddenWordOverSeasData.getId()));
        if (getDb().executeNoneQuery(updateForbiddenWordOverSeasDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertForbiddenWordOverSeasDataSql = "insert into `t_s_forbidden_word_overseas`(id,wordSet) values (?,?)";
    @Override
    protected boolean addData(ForbiddenWordOverSeasData forbiddenWordOverSeasData) {
        forbiddenWordOverSeasData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, forbiddenWordOverSeasData.getId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(forbiddenWordOverSeasData.getWordSet())));
        if (getDb().executeNoneQuery(insertForbiddenWordOverSeasDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ForbiddenWordOverSeasData parseData(ResultSet rs) throws SQLException {
        ForbiddenWordOverSeasData forbiddenWordOverSeasData = new ForbiddenWordOverSeasData();
        forbiddenWordOverSeasData.setId(rs.getInt("id"));
        forbiddenWordOverSeasData.setWordSet(JSON.parseObject(rs.getString("wordSet"), ConcurrentHashSet.class));

        return forbiddenWordOverSeasData;
    }

}
